<template>
  <div class="page">
    <nav-com
      title="提货券转赠"
      :showView="true"
      @back="backHandler()"
    >
    <div
      @click="transferList()"
      style="font-size: 14px;color: #F86931;"
    >转赠记录</div>
  </nav-com>
    <div style="margin: 0px 10px;line-height: 50px;">转赠人手机号：{{userMobile}} </div>
    
    <div
      class="padd_box"
      style="display: flex;margin-top: 10px;margin-right: 10px;margin-left: 10px;align-items: center;justify-content: space-between;"
    >
      <van-col span="12">
        <div class="">
          <van-field
            label-class="label"
            :border="true"
            type="number"
            style=""
            v-model="captcha"
            placeholder="请输入验证码"
          />
        </div>
      </van-col>
      <van-col
        span="10"
        style="text-align: right"
      >
        <div
          class="login_box"
          @click="codeHandler"
          v-if="codeFalg"
          style="text-align: center"
        >
          {{ codeText }}
        </div>
        <div
          class="login_box"
          v-else
          style="text-align: center"
        >
          {{ codeText }}
        </div>
      </van-col>
    </div>
    <van-field
      label-class="label"
      style="margin: 10px 10px;"
      v-model="num"
      placeholder="请输入转赠数量"
      label="转赠数量"
    />

    <van-field
      label-class="label"
      style="margin: 10px 10px;"
      @blur="transferUserHandler"
      v-model="mobile"
      placeholder="请输入对方手机号"
      label="对方手机号"
    />
    <div style="margin: 0px 10px;line-height: 50px;">{{userName}}</div>
    <div
      class="btn"
      @click="transfer"
    >确认转让</div>
  </div>
</template>

<script>
import navCom from "@/component/nav";
import { Toast } from "vant";
import {
  couponIncrease,
  score_send,
  verify_mobile,
  getFttuserUserId2,
} from "@/api/api";
export default {
  name: "index",
  components: {
    navCom,
  },
  data() {
    return {
      mobile: "",
      codeText: "获取验证码",
      codeFalg: true,
      refDisabled: false,
      captcha: "",
      userMobile: "",
      userName: "",
      num: "",
    };
  },
  mounted() {
    getFttuserUserId2().then((res) => {
      console.log("getFttuserUserId2", res);
      if (res.data.code == 1) {
        let cellphone = res.data.data.mobile;
        this.userMobile = cellphone;
      }
    });
  },
  methods: {
    transferList(){
      this.$router.push({name:'ExaGiveOtherRecorde', params:{type:1}})
    },
    backHandler() {
      this.$router.go(-1);
    },

    transferUserHandler() {
      if (!this.mobile.trim()) {
        Toast("手机号不可以为空");
      } else {
        verify_mobile({
          mobile: this.mobile,
        }).then((res) => {
          console.log(res);
          if (res.data.code == 1) {
            this.userName = res.data.data.name;
          } else {
            this.$dialog.alert({
              message: res.data.msg,
            });
          }
        });
      }
    },
    //获取验证码
    codeHandler() {
      let that = this;
      let telLen = this.userMobile.length;
      if (telLen == 11) {
        let time = null;
        clearInterval(time);
        that.codeFalg = false;
        let t = 60;
        time = setInterval(function () {
          t--;
          that.codeText = t;
          if (t <= 0) {
            that.codeText = "获取验证码";
            that.codeFalg = true;
            clearInterval(time);
          }
        }, 1000);

        score_send({
          mobile: this.userMobile,
        }).then((res) => {
          if (res.data.code == 1) {
            Toast("验证码已发送")
          } else {
            this.$dialog.alert({
              message: res.data.msg,
            });
          }
        });
        return true;
      } else {
        this.message = "请输入正确手机号";
        return false;
      }
    },
    async transfer() {
      console.log(this.$route.params.id);
      if (!this.mobile.trim()) {
        Toast("请输入对方手机号");
      } else {
        const res = await couponIncrease({
          library_id: this.$route.params.id,
          mobile: this.mobile,
          sum: this.num,
          captcha: this.captcha,
        });
        Toast(res.data.msg);
        if (res.data.code == 1) {
          this.backHandler();
        }
      }
    },
  },
};
</script>

<style scoped>
.page {
  width: 100vw;
  height: 100vh;
  background: #f5f5f5;
}
.btn {
  width: 93.6vw;
  height: 13.3vw;
  background: #ff8933;
  margin: 0 auto;
  font-size: 4.5vw;
  text-align: center;
  line-height: 13.3vw;
  margin-top: 5.6vw;
  font-family: PingFangSC-Medium, PingFang SC;
  /*font-weight: 500;*/
  color: #ffffff;
  border-radius: 1vw;
}
.label {
  font-size: 4.5vw;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #333333;
}
.login_box {
  /* border: 1px solid #F86931; */
  color: #fff;
  background: #ff8933;
  font-size: 4.2vw;
  border-radius: 10px;
  line-height: 45px;
}
.login_box label {
  color: #fff;
  padding-left: 5.3vw;
}
.login_box input {
  height: 11vw;
  line-height: 11vw;
  width: 50vw;
  border: none;
}
</style>